package com.medical.controller;

import com.medical.entity.FeedbackEntity;
import com.medical.entity.RegistrationEntity;
import com.medical.entity.UserEntity;
import com.medical.entity.dto.FeedbackDto;
import com.medical.entity.dto.UpdateStatusDto;
import com.medical.entity.vo.FeedbackVo;
import com.medical.entity.vo.PrescriptionVo;
import com.medical.service.FeedbackService;
import com.medical.service.RegistrationService;
import com.medical.tool.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    @PostMapping("/add")
    public R add(@RequestBody FeedbackEntity feedbackEntity) {
        try {
            feedbackEntity.setStatus(1);
            feedbackEntity.setCommentTime(new Date());
            feedbackService.add(feedbackEntity);
            return new R(200, "添加成功");
        } catch (Exception e) {
            return new R(500, "添加失败");
        }
    }

    @GetMapping("/list")
    public R selectByIdlist(@RequestParam Integer userId) {
        try {
            List<FeedbackEntity> feedbackEntities = feedbackService.selectByIdlist(userId);
            if (feedbackEntities == null) return new R(400, "查询失败");
            return new R(200, "查询成功!", feedbackEntities);
        } catch (Exception e) {
            return new R(500, "系统出现错误！");
        }
    }

    @GetMapping("/selectList")
    public R selectlist() {
        try {
            List<FeedbackVo> feedbackEntities = feedbackService.selectlist();
            if (feedbackEntities == null) return new R(400, "查询失败");
            return new R(200, "查询成功!", feedbackEntities);
        } catch (Exception e) {
            return new R(500, "系统出现错误！");
        }
    }

    @PostMapping("/reply")
    public R updateStatus(@RequestBody FeedbackDto feedbackDto) {
        feedbackDto.setStatus(2);
        feedbackDto.setReplyTime(new Date());
        // 仅允许从未就医状态转换
        int result = feedbackService.update(feedbackDto);
        if (result > 0) {
            return new R(200, "更新成功");
        } else {
            return new R(400, "更新失败");

        }
    }
}
